package dao;

import bean.Activity;
import bean.ActivityUser;
import bean.PageBean;
import util.JdbcUtils;
import util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * The type Activity user dao.
 *
 * @author yl
 */
public class ActivityUserDao {

    /**
     * 查看活动
     *
     * @param pageBean         the page bean
     * @param activityUser     the activity user
     * @param afterAttendTime  the after attend time
     * @param beforeAttendTime the before attend time
     * @return list
     * @throws Exception the exception
     */
    public List<Object> activityUserList(PageBean pageBean, ActivityUser activityUser, String afterAttendTime, String beforeAttendTime) throws Exception {
        StringBuilder sb = new StringBuilder("select * from activityUser u ");
        List<Object> para = new ArrayList<>();
        if (StringUtil.isNotEmpty(activityUser.getActivityUserId())) {
            sb.append(" and u.activityUserId =?");
            para.add(activityUser.getActivityUserId());
        }
        if (StringUtil.isNotEmpty(activityUser.getUserId())) {
            sb.append(" and u.userId =?");
            para.add(activityUser.getUserId());
        }
        if (StringUtil.isNotEmpty(activityUser.getActivityId())) {
            sb.append(" and u.activityId like ?");
            para.add("%" + activityUser.getActivityId() + "%");
        }

        if (StringUtil.isNotEmpty(afterAttendTime)) {
            sb.append(" and TO_SECONDS(u.attendTime)>=TO_SECONDS(?)");
            para.add(afterAttendTime);
        }
        if (StringUtil.isNotEmpty(beforeAttendTime)) {
            sb.append(" and TO_SECONDS(u.attendTime)<=TO_SECONDS(?)");
            para.add(beforeAttendTime);
        }

        sb.append(" order by u.attendTime desc");
        if (pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getRows());
        }
        String sql = sb.toString().replaceFirst("and", "where");
        return JdbcUtils.queryForArrObject(sql, para, ActivityUser.class);
    }

    /**
     * 查看某个人参加的活动
     *
     * @param pageBean         the page bean
     * @param userId           the user id
     * @param afterAttendTime  the after attend time
     * @param beforeAttendTime the before attend time
     * @return activity list
     * @throws Exception the exception
     */
    public List<Object> getActivityList(PageBean pageBean, String userId, String afterAttendTime, String beforeAttendTime) throws Exception {
        StringBuilder sb = new StringBuilder("select u.activityId, u.userId as userId, title, createTime, startTime, endTime, description, u.status as status, replyContent,replyTime, tempCount, maxUserCount, click, a.userName as userName from activity u,activityUser b,user a where b.userId=a.userId and b.activityId=u.activityId ");
        List<Object> para = new ArrayList<Object>();
        sb.append("and b.userId =").append(userId);
        sb.append(" order by b.attendTime desc");
        if (pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getRows());
        }
        return JdbcUtils.queryForArrObject(sb.toString(), null, Activity.class);
    }

    /**
     * 批量删除活动
     *
     * @param delIds the del ids
     * @return int
     * @throws Exception the exception
     */
    public int activityUserDelete(String delIds) throws Exception {
        String sql = "delete from activityUser where activityUserId in(?)";
        List<Object> para = new ArrayList<Object>();
        para.add(delIds);
        return JdbcUtils.update(sql, para);
    }

    /**
     * 添加活动
     *
     * @param activityUser the activity user
     * @return boolean
     * @throws Exception the exception
     */
    public boolean activityUserAdd(ActivityUser activityUser) throws Exception {
        StringBuffer sb = new StringBuffer("insert into activityUser values(");
        List<Object> para = new ArrayList<Object>();
        if (StringUtil.isNotEmpty(activityUser.getActivityUserId())) {
            sb.append(" ,?");
            para.add(activityUser.getActivityUserId());
        } else if (activityUser.getActivityUserId() == null) {
            sb.append(" ,null");
        }
        if (StringUtil.isNotEmpty(activityUser.getUserId())) {
            sb.append(" ,?");
            para.add(activityUser.getUserId());
        }
        if (StringUtil.isNotEmpty(activityUser.getActivityId())) {
            sb.append(" ,?");
            para.add(activityUser.getActivityId());
        }
        if (StringUtil.isNotEmpty(activityUser.getAttendTime())) {
            sb.append(" ,?");
            para.add(activityUser.getAttendTime());
        }
        sb.append(")");
        String sql = sb.toString().replaceFirst(",", " ");
        System.out.println(sql);
        System.out.println(para);
        return 1 == JdbcUtils.update(sql, para);
    }

    /**
     * Activity user count int.
     *
     * @param activityUser     the activity user
     * @param afterAttendTime  the after attend time
     * @param beforeAttendTime the before attend time
     * @return the int
     * @throws Exception the exception
     */
    public int activityUserCount(ActivityUser activityUser, String afterAttendTime, String beforeAttendTime) throws Exception {
        StringBuffer sb = new StringBuffer("select count(*) as total from activityUser u");

        List<Object> para = new ArrayList<Object>();
        if (StringUtil.isNotEmpty(activityUser.getActivityUserId())) {
            sb.append(" and u.activityUserId =? ");
            para.add(activityUser.getActivityUserId());
        }
        if (StringUtil.isNotEmpty(activityUser.getUserId())) {
            sb.append(" and u.userId =? ");
            para.add(activityUser.getUserId());
        }
        if (StringUtil.isNotEmpty(activityUser.getActivityId())) {
            sb.append(" and u.activityId like ?");
            para.add("%" + activityUser.getActivityId() + "%");
        }
        if (StringUtil.isNotEmpty(afterAttendTime)) {
            sb.append(" and TO_SECONDS(u.attendTime)>=TO_SECONDS(?)");
            para.add(afterAttendTime);
        }
        if (StringUtil.isNotEmpty(beforeAttendTime)) {
            sb.append(" and TO_SECONDS(u.attendTime)<=TO_SECONDS(?)");
            para.add(beforeAttendTime);
        }

        String sql = sb.toString().replaceFirst("and", "where");
        List<Map<String, String>> result = JdbcUtils.queryForMap(sql, para);

        if (!result.isEmpty()) {
            return Integer.parseInt(result.get(0).get("total"));
        } else {
            return 0;
        }
    }
}
